package com.pisolution.scheduler.remote.command;

import java.io.Serializable;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 节点组件之间信息交互的载体 该载体会被序列化之后在客户端与服务端之间进行传输.
 *
 * @author sora
 * @version 0.1.0
 */
public class Command implements Serializable {
    private static final long serialVersionUID = 1L;

    // -------------------------------------------------------------------------------------------------

    /** 用于生成载体的唯一标识. */
    private static final AtomicLong REQUEST_ID = new AtomicLong(1);

    /** 用于区分传输内容是否为命令的幻数. */
    public static final byte MAGIC = (byte) 0x2785;

    // -------------------------------------------------------------------------------------------------

    /** 交互命令类型. */
    private CommandType type;

    /** 用于区分不同命令的标识符. */
    private long opaque;

    /** 用于存储命令的完整内容. */
    private byte[] body;

    // -------------------------------------------------------------------------------------------------
    //  初始化
    // -------------------------------------------------------------------------------------------------

    public Command() {
        this(REQUEST_ID.getAndIncrement());
    }

    public Command(final long opaque) {
        this.opaque = opaque;
    }

    // -------------------------------------------------------------------------------------------------
    //  属性访问
    // -------------------------------------------------------------------------------------------------


    public CommandType getType() {
        return this.type;
    }

    public void setType(CommandType type) {
        this.type = type;
    }

    public long getOpaque() {
        return this.opaque;
    }

    public void setOpaque(long opaque) {
        this.opaque = opaque;
    }

    public byte[] getBody() {
        return this.body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }
}
